var rotate = function (nums, k) {
  let arr = [];
  let len = nums.length;
  if (k % len === 0) {
    return;
  }
  for (let i = 0; i < len; i++) {
    arr[(i + k) % len] = nums[i];
  }
  for (let i = 0; i < len; i++) {
    nums[i] = arr[i];
  }
};

var rotate1 = function (nums, k) {
  let arr = [];
  let len = nums.length;
  if (k % len === 0) {
    return;
  }
  for (let i = 0; i < len; i++) {
    arr[(i + k) % len] = nums[i];
  }
  for (let i = 0; i < len; i++) {
    nums[i] = arr[i];
  }
};

// 优化方法
var rotate = function (nums, k) {
  const n = nums.length;
  k %= n;
  const temp = [...nums];
  for (let i = 0; i < n; i++) {
    nums[(i + k) % n] = temp[i];
  }
};
